package com.opcua.client.mvc.datatree;
import org.eclipse.milo.opcua.stack.core.Identifiers;
import org.eclipse.milo.opcua.stack.core.types.structured.ReferenceDescription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javafx.scene.Node;
import javafx.scene.control.TreeTableCell;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.shape.Rectangle;
import javafx.util.Callback;
public class DataTreeNodeCellFactory
    implements Callback<TreeTableColumn<ReferenceDescription, ReferenceDescription>, TreeTableCell<ReferenceDescription, ReferenceDescription>> {
  protected final static Logger logger = LoggerFactory.getLogger(DataTreeNodeCellFactory.class);
  @Override
  public TreeTableCell<ReferenceDescription, ReferenceDescription> call(TreeTableColumn<ReferenceDescription, ReferenceDescription> param) {
    return new TreeTableCell<ReferenceDescription, ReferenceDescription>() {
      @Override
      protected void updateItem(ReferenceDescription item, boolean empty) {
        super.updateItem(item, empty);
        if (!empty && item != null) {
          setText(item.getDisplayName().getText());
          setGraphic(createGraphicNode(item));
        } else {
          setText(null);
          setGraphic(null);
        }
      }
    };
  }
  private Node createGraphicNode(ReferenceDescription rd) {
    Rectangle rect = new Rectangle(4, 4, 8, 8);
    setStyle(rect, rd);
    return rect;
  }
  private void setStyle(Node node, ReferenceDescription rd) {
    node.getStyleClass().add("tree-icon-" + rd.getNodeClass().toString().toLowerCase());
    if (rd.getTypeDefinition() == null || rd.getTypeDefinition().getIdentifier() == null) {
      return;
    }
    if (Identifiers.PropertyType.getIdentifier().equals(rd.getTypeDefinition().getIdentifier())) {
      node.getStyleClass().add("tree-icon-property");
    } else if (Identifiers.FolderType.getIdentifier().equals(rd.getTypeDefinition().getIdentifier())) {
      node.getStyleClass().add("tree-icon-folder");
    } else if (Identifiers.BaseObjectType.getIdentifier().equals(rd.getTypeDefinition().getIdentifier())) {
      node.getStyleClass().add("tree-icon-baseobject");
    } else if (Identifiers.AnalogItemType.getIdentifier().equals(rd.getTypeDefinition().getIdentifier())) {
      node.getStyleClass().add("tree-icon-analogitem");
    }
  }
}
